package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.blankj.utilcode.util.ToastUtils;
import com.cherry.lib.doc.bean.DocEngine;
import com.cherry.lib.doc.bean.DocSourceType;
import com.cherry.lib.doc.bean.FileType;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPptDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class PptDetailActivity extends BaseAc<ActivityPptDetailBinding> {

    public static StkResBean bean;

    @Override
    protected int onCreate() {
        return R.layout.activity_ppt_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPptDetailBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvCollect.setOnClickListener(this);
        mDataBinding.tvDownload.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCollect://收藏
                List<StkResBean> listSp = AppSpUtils.getCollectList();
                if (bean.isSelected()) {
                    bean.setSelected(false);
                    listSp.remove(bean);
                    AppSpUtils.saveCollectList(listSp);
                } else {
                    if (listSp != null && listSp.size() > 0) {
                        listSp.add(bean);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        listSave.add(bean);
                        AppSpUtils.saveCollectList(listSave);
                    }
                    bean.setSelected(true);
                }
                isCollect();
                break;
            case R.id.tvDownload://下载
                showDialog(getString(R.string.save_ing));
                Downloader.newTask(mContext)
                        .url(bean.getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".ppt")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                dismissDialog();
                                ToastUtils.showShort(getString(R.string.download_suc));
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                dismissDialog();
                                ToastUtils.showShort(R.string.download_def);
                            }
                        });
                break;
        }
    }

    @Override
    protected void initData() {
        /*mDataBinding.webView.setWebViewClient(new AppWebViewClients());
        mDataBinding.webView.getSettings().setJavaScriptEnabled(true);
        mDataBinding.webView.getSettings().setUseWideViewPort(true);
        mDataBinding.webView.loadUrl("http://view.officeapps.live.com/op/view.aspx?src="+bean.getUrl());*/
        mDataBinding.docView.openDoc(this, bean.getUrl(), DocSourceType.URL, -1, false, DocEngine.MICROSOFT);
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            for (StkResBean stkResBean : listSp) {
                if (stkResBean.equals(bean)) {
                    bean.setSelected(true);
                    break;
                }
            }
        }
        isCollect();
    }

    private void isCollect() {
        if (bean.isSelected()) {
            mDataBinding.tvCollect.setText(R.string.cancel_collect);
            mDataBinding.tvCollect.setBackgroundResource(R.drawable.an3);
        } else {
            mDataBinding.tvCollect.setText(R.string.collect_template);
            mDataBinding.tvCollect.setBackgroundResource(R.drawable.an1);
        }
    }

    private class AppWebViewClients extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }
    }

}